package com.ithuangyl.common.net.nettest;

import android.text.TextUtils;

import com.ithuangyl.common.config.ConfigKeys;
import com.ithuangyl.common.config.Latte;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/7/11 19:59
 */
public class RestCreator {


    private RestCreator() {

    }

    private static class RestCreatorHolder {
        private static final RestCreator REST_CREATOR = new RestCreator();
    }

    public static RestCreator get() {
        return RestCreatorHolder.REST_CREATOR;
    }

    public static Retrofit getRetrofit() {
        return RetrofitCreator.get();
    }

    public static Retrofit getRetrofit(String baseUrl) {
        return RetrofitCreator.get(baseUrl);
    }


    /**
     * 构建OkHttp
     */
    private static final class OKHttpHolder {
        private static final int TIME_OUT = 60;
        private static final OkHttpClient.Builder BUILDER = new OkHttpClient.Builder();
        private static final ArrayList<Interceptor> INTERCEPTORS = Latte.getConfiguration(ConfigKeys.INTERCEPTOR);

        private static OkHttpClient.Builder addInterceptor() {
            if (INTERCEPTORS != null && !INTERCEPTORS.isEmpty()) {
                for (Interceptor interceptor : INTERCEPTORS) {
                    BUILDER.addInterceptor(interceptor);
                }
            }
            return BUILDER;
        }

        private static final OkHttpClient OK_HTTP_CLIENT = addInterceptor()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .build();
    }


    /**
     * 构建全局Retrofit客户端
     */
    private static final class RetrofitCreator {
        private static HashMap<String, Retrofit> RETROFIT_MAP = new HashMap<>();

        private static final String BASE_URL = Latte.getConfiguration(ConfigKeys.IS_DEBUG) ?
                Latte.getConfiguration(ConfigKeys.DEBUG_API_HOST) :
                Latte.getConfiguration(ConfigKeys.RELEASE_API_HOST);


        /**
         * 适用于app有多个接口域名的情况
         *
         * @param baseUrl
         * @return
         */
        public static Retrofit get(String baseUrl) {
            if (!TextUtils.isEmpty(baseUrl)) {
                Retrofit retrofit = RETROFIT_MAP.get(baseUrl);
                if (retrofit == null) {
                    Retrofit.Builder builder = new Retrofit.Builder();
                    retrofit = builder
                            .baseUrl(baseUrl)
                            .client(OKHttpHolder.OK_HTTP_CLIENT)
                            .addConverterFactory(ScalarsConverterFactory.create())
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    RETROFIT_MAP.put(baseUrl, retrofit);
                }
                return retrofit;
            }
            throw new NullPointerException("baseUrl is null");
        }

        /**
         * 获取默认Retrofit
         *
         * @return
         */
        public static Retrofit get() {
            if (!TextUtils.isEmpty(BASE_URL)) {
                Retrofit retrofit = RETROFIT_MAP.get(BASE_URL);
                if (retrofit == null) {
                    Retrofit.Builder builder = new Retrofit.Builder();
                    retrofit = builder
                            .baseUrl(BASE_URL)
                            .client(OKHttpHolder.OK_HTTP_CLIENT)
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    RETROFIT_MAP.put("retrofit", retrofit);
                }
                return retrofit;
            }
            throw new NullPointerException("baseUrl is null,please call Configurator.withApiHost() to init");
        }
    }

}
